// Magic Software, Inc.
// http://www.magic-software.com
// Copyright (c) 2000, All Rights Reserved
//
// Source code from Magic Software is supplied under the terms of a license
// agreement and may not be copied or disclosed except in accordance with the
// terms of that agreement.  The various license agreements may be found at
// the Magic Software web site.  This file is subject to the license
//
// RESTRICTED USE SOURCE CODE
// http://www.magic-software.com/License/restricted.pdf

#include "MgcRenderer.h"
#include "MgcPolyline.h"

MgcImplementRTTI(MgcPolyline,MgcGeometry);
MgcImplementStream(MgcPolyline);

//---------------------------------------------------------------------------
MgcPolyline::MgcPolyline (unsigned int uiVertexQuantity,
    MgcVector3* akVertex, MgcVector3* akNormal, MgcColor* akColor,
    MgcVector2* akTexture, bool bClosed)
    :
    MgcGeometry(uiVertexQuantity,akVertex,akNormal,akColor,akTexture)
{
    assert( uiVertexQuantity >= 2 );

    m_bClosed = bClosed;
}
//---------------------------------------------------------------------------
MgcPolyline::MgcPolyline ()
{
    m_bClosed = false;
}
//---------------------------------------------------------------------------
void MgcPolyline::Reconstruct (unsigned int uiVertexQuantity,
    MgcVector3* akVertex, MgcVector3* akNormal, MgcColor* akColor,
    MgcVector2* akTexture, bool bClosed)
{
    MgcGeometry::Reconstruct(uiVertexQuantity,akVertex,akNormal,akColor,
        akTexture);
    m_bClosed = bClosed;
}
//---------------------------------------------------------------------------
void MgcPolyline::GetSegment (unsigned int uiL, MgcVector3& rkV0,
    MgcVector3& rkV1) const
{
    if ( m_bClosed )
    {
        assert( uiL < m_uiVertexQuantity );
        rkV0 = m_akVertex[uiL];
        if ( uiL != m_uiVertexQuantity - 1 )
            rkV1 = m_akVertex[uiL+1];
        else
            rkV1 = m_akVertex[0];
    }
    else
    {
        assert( uiL < m_uiVertexQuantity - 1 );
        rkV0 = m_akVertex[uiL];
        rkV1 = m_akVertex[uiL+1];
    }
}
//---------------------------------------------------------------------------
void MgcPolyline::UpdateModelNormals ()
{
    // TO DO.  A vertex normal is the bisector of the normals for the two
    // segments meeting at that vertex.
}
//---------------------------------------------------------------------------
void MgcPolyline::Draw (MgcRenderer& rkRenderer)
{
    MgcGeometry::Draw(rkRenderer);
    rkRenderer.Draw(*this);
}
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// streaming
//---------------------------------------------------------------------------
MgcObject* MgcPolyline::Factory (MgcStream& rkStream)
{
    MgcPolyline* pkObject = new MgcPolyline;
    MgcStream::Link* pkLink = new MgcStream::Link(pkObject);
    pkObject->Load(rkStream,pkLink);
    return pkObject;
}
//---------------------------------------------------------------------------
void MgcPolyline::Load (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcGeometry::Load(rkStream,pkLink);

    // native data
    MgcStreamRead(rkStream,m_bClosed);
}
//---------------------------------------------------------------------------
void MgcPolyline::Link (MgcStream& rkStream, MgcStream::Link* pkLink)
{
    MgcGeometry::Link(rkStream,pkLink);
}
//---------------------------------------------------------------------------
bool MgcPolyline::Register (MgcStream& rkStream)
{
    return MgcGeometry::Register(rkStream);
}
//---------------------------------------------------------------------------
void MgcPolyline::Save (MgcStream& rkStream)
{
    MgcGeometry::Save(rkStream);

    // native data
    MgcStreamWrite(rkStream,m_bClosed);
}
//---------------------------------------------------------------------------
